
/** *****************  JAVA头文件说明  ****************
 * file name  :  ValidateCodeServlet.java
 * owner      :  yangjisong@umpay.com
 * copyright  :  UMPAY
 * description:  
 * modified   :  2014-3-10
 * *************************************************/ 

package com.busi.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/** ******************  类说明  *********************
 * class       :  ValidateCodeServlet
 * @author     :  yangjisong@umpay.com
 * @version    :  1.0  
 * description :  
 * @see        :                        
 * ************************************************/


/**
 * 产生随机验证码图像
 * @cmopany:    UMPAY
 * @Description:
 * @author:     JL
 * @date:       Aug 16, 2009 5:01:32 PM 
 * @version     V1.0
 */
public class ValidateCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int width = 60, height = 20;
		BufferedImage image = new BufferedImage(width, height,	BufferedImage.TYPE_INT_RGB);
	    Graphics g = image.createGraphics(); 

		Random random = new Random(System.currentTimeMillis());
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);

		// 随机产生若干条干扰线，使图象中的认证码不易被其它程序探测到	
		g.setColor(getRandColor(random, 160, 200));
		for (int i = 0; i < 40; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			g.drawLine(x, y, x + xl, y + yl);
		}
		
		String sRand = "";
		for (int i = 0; i < 4; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
			//改变字体大小及是否倾斜
			if (random.nextInt(6) > 3) {
				Font a = new Font("Times New Roman", Font.BOLD, 15 + random.nextInt(4));
				g.setFont(a);
			} else {
				Font a = new Font("Times New Roman", Font.BOLD + Font.ITALIC, 15 + random.nextInt(4));
				g.setFont(a);
			}
			//g.setClip(x, y, width, height)
			//g.rotate(10);
			g.drawString(rand, 13 * i + 3 + random.nextInt(5), 16 + random.nextInt(5));
		}
		g.dispose();
		//验证码存放到Session
		request.getSession(true).setAttribute("verifycode", sRand);
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		try {
//			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
//			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
//			param.setQuality(1.0f, false);
//			encoder.setJPEGEncodeParam(param);
//			encoder.encode(image);
			ImageIO.write(image, "JPEG", response.getOutputStream());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private Color getRandColor(Random random, int fc, int bc) {
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}
}
